package org.hang.util;

import org.hang.exception.ResponseCode;

//Ajax请求响应对象的类
public class AjaxResult {
    //private boolean success = true;
    //private String message = "操作成功!";

//    private Integer code = ResponseCode.RESPONSE_CODE_200.getCode();     // 默认200，表示操作成功
//    private Boolean success = true;    // 表示当前操作是成功还是失败 true:成功 false:失败
//    private String message = ResponseCode.RESPONSE_CODE_200.getMsg();     // 失败的原因
//    private Object data;   // 返回给前端的查询结果对象,是一定不能把类型写死的,因为这个AjaxResult类是公共的,每个controller都是返回AjaxResult类的

    private Integer code = 200;
    private boolean success = true;
    private String message = "操作成功!";

    public static AjaxResult success(){
        AjaxResult ajaxResult = new AjaxResult();
        return ajaxResult;
    }

    //返回到前台对象
    private Object resultObj;

    public boolean isSuccess() {
        return success;
    }

    public AjaxResult setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public AjaxResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getResultObj() {
        return resultObj;
    }

    public AjaxResult setResultObj(Object resultObj) {
        this.resultObj = resultObj;
        return this;
    }

    //AjaxResult.me()成功
    //AjaxResult.me().setMessage()成功
    //AjaxResult.me().setSuccess(false),setMessage("失败");
    public  static AjaxResult me(){
        return new AjaxResult();
    }



    public static AjaxResult error(ResponseCode responseCode){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        //ajaxResult.setCode(responseCode.getCode());
        ajaxResult.setMessage(responseCode.getMsg());
        return ajaxResult;
    }

    public static AjaxResult error(Integer code,String msg){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        //ajaxResult.setCode(code);
        ajaxResult.setMessage(msg);
        return ajaxResult;
    }

//    public AjaxResult setData(Object data){
//        this.data=data;
//        return this;
//    }

    public Integer getCode() {
        return code;
    }

    public AjaxResult setCode(Integer code) {
        this.code = code;
        return this;
    }

   /*
    //成功
    public AjaxResult() {
    }

    //失败并且有提示
    public AjaxResult(String message) {
        this.success = false;
        this.message = message;
    }*/
}
